/*
 * @Author: 肖思汗 
 * @Date: 2025-06-25 09:26:19 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-07-19 16:24:15
 */

import { autorun, makeAutoObservable } from "mobx";
import { TemplateData, TemplateGroupData } from "types/type";
import templateModelApi from "./templateModelApi";
import { get } from "lodash";
import userStore from "src/pages/User/userStore";

export interface Option {
    value: string | number;
    label: string;
    children?: Option[];
}

class TemplateModelStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
        autorun(() => {
            if (userStore.isLogin) {
                this.getTemplateGroup();
            }
        }, { delay: 30 });
    }

    show = false;
    setShow(show: boolean) {
        this.show = show;
    }

    // 模版的分组
    cascaderOptions: Array<Option> = [];
    async getTemplateGroup() {
        const result = await templateModelApi.listTemplateGroup()
        const cascaderOptions: Array<Option> =
            (result.rows as Array<TemplateGroupData>)
                .filter(g => g.parentId === 0)
                .map(g => ({
                    value: g.templateGroupId,
                    label: g.groupName,
                    children: (g.children || []).map(c => ({
                        value: c.templateGroupId,
                        label: c.groupName
                    }))
                }));

        this.cascaderOptions = cascaderOptions;
        this.setActiveid([
            get(cascaderOptions, [0, "value"]) as number,
            get(cascaderOptions, [0, "children", 0, "value"]) as number
        ]);
    }

    // 当前选中的分组    activeid: [0, 0],
    activeid: [number, number | undefined] = [0, 0];
    setActiveid(activeid: [number, number | undefined]) {
        this.activeid = activeid;
        this.getNextPage(true);
    }


    //加载中
    loading = false;
    setLoading(loading: boolean) {
        this.loading = loading;
    }
    page = 1;
    pageSize: number = 16;
    hasNext: boolean = true;
    // 素材列表
    templates: Array<TemplateData> = [];
    async getNextPage(reset: boolean = false) {
        if (reset) {
            this.page = 1;
            this.hasNext = true;
        } else if (!this.hasNext || this.loading) {
            return;
        } else {
            this.page++;
        }
        this.loading = true;

        if (!this.activeid[1]) {
            this.hasNext = false;
            this.templates = [];
            return;
        }

        const result = await templateModelApi.getTemplateList({
            templateGroupId: this.activeid[1],
            pageNum: this.page,
            pageSize: this.pageSize
        });

        if (result?.rows) {

            if (reset) {
                this.templates = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.templates = [...result.rows];
            } else {
                this.templates = [...this.templates, ...result.rows];
            }
            this.hasNext = (result.rows.length === this.pageSize);
            this.loading = false;

        }

    }



}

const templateModelStore = new TemplateModelStore();

export default templateModelStore;